/*
 * @Descripttion: 
 * @version: 请写项目版本
 * @Author: @cdm
 * @Date: 2021-10-12 15:17:11
 * @LastEditors: @cdm
 * @LastEditTime: 2022-03-09 13:54:10
 * No pains no gains
 */

'use strict';
const app = require('tcb-admin-node');
const tcb_config = {env: 'life-9gekhhx1136c3452'};
app.init(tcb_config).auth();
const db = app.database();
exports.main = async (event, context, callback) => {
  let { action } = event;
  switch(action) {
    case 'getRank': //获取排名
      return await db.collection('rank').orderBy('money','desc').limit(100).get();
    case 'upRank': //上传排名
      let myRank = await db.collection('rank').where({name: event.params.name}).get();
      if(myRank.data.length > 0) {
        await db.collection('rank').where({name: event.params.name}).update(event.params);
        return await db.collection('rank').get();
      }else {
        return await db.collection('rank').add(event.params);
      }
    case 'getName'://获取我的排名
      let tmpRank = await db.collection('rank').where({name: event.params.name}).get();
      if(tmpRank.data && tmpRank.data.length > 0) {
        return false;
      }else {
        return true;
      }
    case "setName": //更新排名
      return await db.collection('rank').add(event.params);

  }
};
